{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "714cb2f2-3372-45f6-ad86-4bf1c4b34d83",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-29T13:14:01.665687Z",
     "iopub.status.busy": "2023-11-29T13:14:01.665094Z",
     "iopub.status.idle": "2023-11-29T13:14:18.535429Z",
     "shell.execute_reply": "2023-11-29T13:14:18.534517Z",
     "shell.execute_reply.started": "2023-11-29T13:14:01.665653Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.zhipin.com/job_detail/?query=新媒体运营&city=101280100&page=1\n",
      "https://www.zhipin.com/job_detail/?query=新媒体运营&city=101280100&page=2\n",
      "https://www.zhipin.com/job_detail/?query=新媒体运营&city=101280100&page=3\n",
      "https://www.zhipin.com/job_detail/?query=新媒体运营&city=101280100&page=4\n",
      "https://www.zhipin.com/job_detail/?query=新媒体运营&city=101280100&page=5\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import csv\n",
    "import time\n",
    "import pandas as pd\n",
    "\n",
    "def get_job_list(keyword, city, pages=5):\n",
    "    job_list = []\n",
    "\n",
    "    base_url = f'https://www.zhipin.com/job_detail/?query={keyword}&city={city}'\n",
    "    headers = {\n",
    "    'authority': 'www.zhipin.com',\n",
    "    'method': 'GET',\n",
    "    'path': '/wapi/zpgeek/webtopbanner/query.json',\n",
    "    'scheme': 'https',\n",
    "    'accept': '*/*',\n",
    "    'accept-encoding': 'gzip, deflate, br',\n",
    "    'accept-language': 'zh-CN,zh;q=0.9',\n",
    "    'cache-control': 'no-cache',\n",
    "    'Cookie':'lastCity=101280100;wd_guid=956f6b07-803a-4187-b2f6-20e0fb739351;historyState=state; _bl_uid=60lqaivRhO754aozIpqIwjg774p2; boss_login_mode=sms; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1701257468; __zp_seo_uuid__=c4c418b5-590d-403f-9590-1739e8e6a07b; __g=-; __l=r=https%3A%2F%2Faistudio.baidu.com%2F&l=%2Fwww.zhipin.com%2Fjob_detail%2F%3Fquery%3D%25E8%25BF%2590%25E8%2590%25A5%26city%3D101280100&s=1&g=&s=3&friend_source=0; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1701257550; wt2=D4JIWD8EASykYWQjTP9hR6FpWXbSn8I2Xgekn8JlBVW4mwnRJOlJnCa1Ls26ej9GNFp2wuJ7KWhPvKn1JRKMjvg~~; wbg=0; __zp_stoken__=6919fw4XDhsO9DzcLBg14DQvCvW9qVMK7Z2VTS3HCrXBbS0dcwqvCpMKrR8KNwrdbWU3CqsKJb8KFwqpGYMKhwq3CsMK%2FwpZXw4BCw7BYwpbEmcO%2BwqbDgcKXw7zCs8KVNywICBAHBA8PBxALAwMCDQ4ICBAHBAkJEQYFNi%2FDu8O0Lzg5NDQpSkpCAk1bYEtfSQVWSkM3N1xeBgU3LSg0NzPCvMKbwrkZwrzCk8K%2BEsOAwpPCtsK%2FND8zM8OAw7wiJMKzw4MIwrR3BzM0wrfCp1YRCMOHXFogXsK1Gy9BN8K2PDgdOjQ4OkA3Pzg4LTbCt8Kqw4NUXRRhwrkRJzcYQEA3PTY4QDc7ODosNz8%2FIkA0JzwHEQcMCyk1wrhXwrPDl0A3; __c=1701256680; __a=36210370.1685866999.1685866999.1701256680.13.2.6.13; geek_zp_token=V1RNglEeX43FxgXdJpzh8dISqy6jnewA~~',\n",
    "    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',\n",
    "    'x-requested-with': 'XMLHttpRequest',\n",
    "    }\n",
    "\n",
    "    for page in range(1, pages + 1):\n",
    "        url = f'{base_url}&page={page}'\n",
    "        response = requests.get(url, headers=headers)\n",
    "        \n",
    "        # 添加延时等待页面加载\n",
    "        time.sleep(3)\n",
    "        print(url)\n",
    "\n",
    "        soup = BeautifulSoup(response.text, 'html.parser')\n",
    "        jobs = soup.find_all('div', class_='primary-box')\n",
    "\n",
    "        for job in jobs:\n",
    "            title_tag = job.find('span', class_='job-name').find('a')\n",
    "            title = title_tag.get('title') if title_tag else 'N/A'\n",
    "            company = job.find('div', class_='job-limit clearfix').find('div', class_='info-publis').find('h3').text.strip()\n",
    "            \n",
    "            salary_tag = job.find('div', class_='job-limit clearfix').find('span', class_='red')\n",
    "            salary = salary_tag.text.strip() if salary_tag else 'N/A'\n",
    "            \n",
    "            job_info = f'{title} | {company} | {salary}'\n",
    "            job_list.append(job_info)\n",
    "\n",
    "    return job_list\n",
    "\n",
    "keyword = '新媒体运营'\n",
    "city = 101280100\n",
    "\n",
    "# 获取5页的数据\n",
    "job_list = get_job_list(keyword, city, pages=5)\n",
    "\n",
    "# 将数据拆分为职位名称、招聘人和薪资\n",
    "job_data = [job.split(' | ') for job in job_list]\n",
    "positions, recruiters, salaries = zip(*job_data)\n",
    "\n",
    "# 创建 DataFrame\n",
    "df = pd.DataFrame({'Position': positions, 'Recruiter': recruiters, 'Salary': salaries})\n",
    "\n",
    "# 将 DataFrame 写入 CSV 文件\n",
    "df.to_csv('job_list.csv', index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b8c727a-d53f-4f74-821e-e873dc0c24a9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
